package mySemantic;

import java.util.ArrayList;
import java.util.List;

/**
 * 符号表
 * 分为不同类型： Global, Local, loopTemp, conditionTemp
 */


public class SymbolTable {
    enum TYPE
    {
        GLOBAL,
        LOCAL,
        LOOPTEMP,
    }

    public TYPE type;
    public List<Symbol> symbolList;

    public SymbolTable(TYPE type)
    {
        this.type = type;
        this.symbolList = new ArrayList<>();
    }

    public Symbol getSymbol(String id)
    {
        for(Symbol symbol : symbolList)
        {
            if(symbol.id.equals(id))
            {
                return symbol;
            }
        }
        return null;
    }

    public Symbol getLastOne()
    {
        if(symbolList.size() != 0)
        {
            return symbolList.get(symbolList.size() - 1);
        }
        return null;
    }

    public Symbol popLastOne()
    {
        if(symbolList.size() != 0)
        {
            Symbol symbol = symbolList.get(symbolList.size() - 1);
            symbolList.remove(symbol);
            return symbol;
        }
        return null;
    }

    public void setLastOne(Symbol symbol)
    {
        if(symbolList.size() != 0) {
            symbolList.set(symbolList.size() - 1, symbol);
        }
        else
        {
            symbolList.add(symbol);
        }
    }

    public void add(Symbol symbol)
    {
        symbolList.add(symbol);
    }

    public void clear()
    {
        this.symbolList.clear();
    }

}
